package com.qf.Dao.impl;

import com.qf.Dao.InformDao;
import com.qf.Utils.DBUtils;
import com.qf.entity.SysNotice;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class InformDaoImpl implements InformDao {
    private QueryRunner qr = new QueryRunner(DBUtils.DB.getDs());

    /**
     * 根据id查询全部内容
     *
     * @param id
     * @return
     * @throws SQLException
     */

    @Override
    public List<SysNotice> selectNotice(int id) throws SQLException {
        String sql = "select * from sys_notice where id =? ";
        List<SysNotice> sysNotices = qr.query(sql, new BeanListHandler<SysNotice>(SysNotice.class), id);
        return sysNotices;
    }

    @Override
    public int deleteNotice(String title) throws SQLException {
        String sql = "DELETE FROM sys_notice  WHERE title = ?";
        Object obj = title;
        int update = qr.update(sql, obj);
        return update;
    }

    @Override
    public int addNotice(SysNotice sysNotice) throws SQLException {
        String sql = "INSERT INTO sys_notice (id,title,type,content,createtime,creater) VALUES(?,?,?,?,?,?)";
        Object[] objects = {sysNotice.getId(), sysNotice.getTitle(), sysNotice.getContent(), sysNotice.getCreater(), sysNotice.getCreatetime(), sysNotice.getCreater()};
        int update = qr.update(sql, objects);
        return update;
    }

    @Override
    public int updateNotice(SysNotice sysNotice) throws SQLException {
        String sql = "UPDATE sys_notice SET title= ? , type= ? ,content = ?,createtime = ? ,creater = ?WHERE id= ? ";
        Object[] objects = {sysNotice.getTitle(), sysNotice.getContent(), sysNotice.getCreater(), sysNotice.getCreatetime(), sysNotice.getCreater(), sysNotice.getId()};
        int update = qr.update(sql, objects);
        return update;
    }

}
